/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package idp.dashboard;

import idp.communication.CommunicationController;
import idp.communication.MessageController;
import idp.exceptions.UserCanceledActionException;
import idp.settings.SettingModel;
import idp.utils.DateFormatter;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * 
 * @author M Dekker
 */
public class MainFrame extends JFrame {

	private static MainFrame _instance;
    private BufferedImage imgBackground;
    
    protected String _matchName;
    protected int _matchID = 1;
    
    public JButton replayButton,recordButton,reconnectButton;
    public Vector<String> matches = new Vector();
    public MainPanel dashboard;
    
    /**
     * Constructor
     */
    private MainFrame()
    {
    	super("Fikings Dashboard");
    	
    	this.addWindowListener(new WindowListener(){

			public void windowActivated(WindowEvent e){}
			public void windowClosed(WindowEvent e){}

			public void windowClosing(WindowEvent e) {
				if (CommunicationController.getInstance() != null) {
					CommunicationController.getInstance().Quit();
				}
				try {
					Thread.sleep(1000l);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.exit(0);
			}

			public void windowDeactivated(WindowEvent e){}
			public void windowDeiconified(WindowEvent e){}
			public void windowIconified(WindowEvent e){}
			public void windowOpened(WindowEvent e){}
    		
    	});
    	
    	imgBackground = MainPanel.loadImage("lib/img/background.jpg");
    	//FlowLayout layout = new FlowLayout();
        this.setLayout(null);
        
		final JComboBox matchCombo = new JComboBox(matches);
		matchCombo.setBounds(320, 5, 140, 30);
		this.add(matchCombo);

		reconnectButton = new JButton("Verbind");
		reconnectButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JButton source = (JButton) e.getSource();
				if(source.getText().equals("Verbind"))
				{
					if(CommunicationController.getInstance().getState() == Thread.State.NEW)
						CommunicationController.getInstance().start();

					source.setEnabled(false);
				} else {
					System.out.println("quiting communication controller");
					CommunicationController.getInstance().Quit();
				}
			}
		});
		reconnectButton.setBounds(10, 5, 120, 30);
		this.add(reconnectButton);

		recordButton = new JButton("Start Wedstrijd");
        recordButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e)
            {
                JButton source = (JButton) e.getSource();
            	if(source.getText().equals("Start Wedstrijd"))
            	{
            		_matchName = DateFormatter.formatTime(System.currentTimeMillis());
            		MainFrame.getInstance().replayButton.setEnabled(false);
            		DataController.getInstance().startMatchRecording(_matchName);
            		source.setText("Stop Wedstrijd");
            	}
            	else
            	{
            		MainFrame.getInstance().replayButton.setEnabled(true);
            		matches.add(_matchName);
            		matchCombo.setSelectedItem(_matchName);
            		DataController.getInstance().stopMatchRecording();
            		source.setText("Start Wedstrijd");

				}
			}

        });
        recordButton.setBounds(150,5,150,30);
        this.add(recordButton);

		replayButton = new JButton("Start Terugblik");
		replayButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JButton source = (JButton) e.getSource();
				if (source.getText() == "Start Terugblik") {
					DataController.getInstance().startMatchReplay(
							(String) matchCombo.getSelectedItem());
					if (MessageController.getInstance() != null
							&& dashboard != null)
						MessageController.getInstance().removeListener(
								dashboard);
					source.setText("Stop Terugblik");
					MainFrame.getInstance().recordButton.setEnabled(false);
				} else {
					DataController.getInstance().stopMatchReplay();
					if (MessageController.getInstance() != null
							&& dashboard != null)
						MessageController.getInstance().addListener(dashboard);

					source.setText("Start Terugblik");
					MainFrame.getInstance().recordButton.setEnabled(true);
				}
			}

		});
		replayButton.setBounds(460, 5, 160, 30);
		this.add(replayButton);

		JButton deleteReplayButton = new JButton("Verwijder Terugblik");
		deleteReplayButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				DataController.getInstance().deleteMatch(
						(String) matchCombo.getSelectedItem());
				matchCombo.removeItem(matchCombo.getSelectedItem());
			}

		});
        deleteReplayButton.setBounds(610,5,180,30);
        this.add(deleteReplayButton);
        
        JLabel intervalLabel = new JLabel("Interval (ms):");
        intervalLabel.setBounds(810, 5, 100, 30);
        this.add(intervalLabel);
        
        String[] intervals = {"50","100","500","1000","2000"};
        final JComboBox intervalCombo = new JComboBox(intervals);
        intervalCombo.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e)
			{
				JComboBox source = (JComboBox) e.getSource();
				SettingModel.LoadByKey("UPDATE_INTERVAL").setValue((String) source.getSelectedItem());
				
			}});
        intervalCombo.setBounds(890, 5, 140, 30);
        this.add(intervalCombo);
        
        
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        this.setLocation((screenSize.width-420) / 4, (screenSize.height-300) / 4);
        
        dashboard = new MainPanel();
        dashboard.setBounds(0, 40, 1040, 600);
        this.add(dashboard);
        
        this.setResizable(false);
        this.setSize(1040,600);
        this.setVisible(true);
    }
    
	/**
	 * Returns the current instance of MainFrame
	 * @return
	 */
    public static MainFrame getInstance(){
    	if(_instance == null) _instance = new MainFrame();
    	return _instance;
    }
}
